package com.example.graduate.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.example.graduate.env.RedisKeyConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.UUID;

import static com.example.graduate.env.RedisKeyConstants.AUTHENTICATION_USER;

/**
 * 随机生成 UUID 工具类
 */
@Component
public class UuidUtils {

    @Autowired
    RedisUtil redisUtil;

    public static String userId;


    public String getUUID(){
        return UUID.randomUUID().toString().replace("-", "");
    }


    public String getToken(String userId){
        String uuid = getUUID();
        String token="";
        token= JWT.create().withAudience(userId)
                .sign(Algorithm.HMAC256(uuid));
        redisUtil.hPut(RedisKeyConstants.AUTHENTICATION_USER,userId,uuid);
        return token;
    }

    /**
     * 随机生成六位验证码
     * @return
     */
    public String getCode(){
      int code = (int)((Math.random()*9+1)*100000);
      return String.valueOf(code);
    }

}
